package com.swgk.core.dialog;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.swgk.core.R;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.MultiChildAdapter;
import com.swgk.core.recyclerview.adapter.MultiParentAdapter;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;

import java.util.List;

/**
 * 市区二级选择器
 * 只包含市+区二级的展示
 * setCityList 二级数据源时使用
 * setCitySelectType 必须设置展示风格
 * setAreaSelectType 必须设置展示风格
 * <p>
 * Created by Andy on 2021/2/5.
 *
 * @param <D>
 */
public class MultiLevelAreaPopWindow<D> extends BasePopWindow {
    private RecyclerView rvParent;//父级列表控件
    private RecyclerView rvChild;//子级列表控件

    private MultiParentAdapter parentAdapter;
    private MultiChildAdapter childAdapter;
    private TextView mTvReSet;
    private TextView mTvOk;

    private boolean isDefaultFirstCity = false;
    private boolean isDefaultFirstArea = false;

    private BaseAreaEntity parentEntity = new BaseAreaEntity();

    public MultiLevelAreaPopWindow(Context context) {
        super(context);
    }

    public void setCityList(List<BaseAreaEntity<List<BaseAreaEntity>>> cityList) {
        parentAdapter.setDatas(cityList);
        childAdapter.setDatas(cityList.get(0).getChildren());
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_select_multi;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rvParent = holder.getPop().findViewById(R.id.rl_parent);
        rvChild = holder.getPop().findViewById(R.id.rl_child);
        mTvReSet = holder.getPop().findViewById(R.id.tv_re_set);
        mTvOk = holder.getPop().findViewById(R.id.tv_ok);

        parentAdapter = new MultiParentAdapter(mContext, null, R.layout.item_multi_parent);
        childAdapter = new MultiChildAdapter(mContext, null, R.layout.item_multi_child);
        rvParent.setAdapter(parentAdapter);
        rvChild.setAdapter(childAdapter);
        rvParent.setLayoutManager(new LinearLayoutManager(mContext));
        rvChild.setLayoutManager(new LinearLayoutManager(mContext));

        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    onResultClick.result(null,
                            parentEntity,
                            getCheckDatas());
                }
                dismiss();
            }
        });

        mTvReSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetData();
                if (isDefaultFirstCity) {
                    parentAdapter.setDefItem(0);
                    parentAdapter.setCheckDefault(0);
                }
                if (isDefaultFirstArea) {
                    childAdapter.setDefItem(0);
                    childAdapter.setCheckDefault(0);
                }
                if (listener != null) {
                    listener.reSet();
                    dismiss();
                }
            }
        });

    }


    //处理不限的参数,确保no不为空字符串
    public List<BaseAreaEntity> getCheckDatas() {
        if (childAdapter.getCheckDatas() != null && childAdapter.getCheckDatas().size() > 0) {
            if (TextUtils.isEmpty(childAdapter.getCheckDatas().get(0).getNo())) {
                childAdapter.getCheckDatas().remove(0);
            }
        }
        return childAdapter.getCheckDatas();
    }

    /**
     * 设置二级列表单选多选
     *
     * @param isDefaultFirst 是否默认选中当前第一项
     */
    public void setCitySelectType(boolean isDefaultFirst) {
        this.isDefaultFirstCity = isDefaultFirst;
        if (isDefaultFirst) {
            parentAdapter.setDefItem(0);
            parentAdapter.setCheckDefault(0);
            if (parentAdapter.getDatas().size() > 0) {
                parentEntity = parentAdapter.getDatas().get(0);
            }
        }
        parentAdapter.setItemSelect(true);
        parentAdapter.setOnItemClick(new AdapterOnItemClick<BaseAreaEntity<List<BaseAreaEntity>>>() {
            @Override
            public void onItemClick(BaseAreaEntity<List<BaseAreaEntity>> listBaseAreaEntity, int position) {
                parentEntity = (BaseAreaEntity) listBaseAreaEntity;
                childAdapter.setDatas(listBaseAreaEntity.getChildren());
                childAdapter.resetCheckDatas();
                if (isDefaultFirst) {
                    childAdapter.setDefItem(0);
                    childAdapter.setCheckDefault(0);
                }
            }
        });
    }

    /**
     * 设置三级表单选多选
     *
     * @param isMultiSelect  true多选；false单选
     * @param isDefaultFirst 是否默认选中当前第一项
     */
    public void setAreaSelectType(boolean isMultiSelect, boolean isDefaultFirst) {
        this.isDefaultFirstArea = isDefaultFirst;
        if (isDefaultFirst) {
            childAdapter.setDefItem(0);
            childAdapter.setCheckDefault(0);
        }

        if (isMultiSelect) {
            childAdapter.setMultiSelect(true);
            childAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<BaseAreaEntity>() {
                @Override
                public void onItemMultiClick(List<BaseAreaEntity> list, int position) {
                    if (position == 0 && TextUtils.isEmpty(childAdapter.getDatas().get(0).getNo())) {
                        childAdapter.resetCheckDatas();
                        childAdapter.setCheckDefault(0);
                        childAdapter.setDefItem(0);
                    } else if (list.size() > 0 && TextUtils.isEmpty(list.get(0).getNo())) {
                        childAdapter.resetCheckDatas();
                        childAdapter.setCheckDefault(position);
                        childAdapter.setDefItem(position);
                    }
                }
            });
        } else {
            childAdapter.setItemSelect(true);
            childAdapter.setOnItemClick(new AdapterOnItemClick<BaseAreaEntity>() {
                @Override
                public void onItemClick(BaseAreaEntity baseAreaEntity, int position) {
                    childAdapter.resetCheckDatas();
                    childAdapter.setCheckDefault(position);
                    childAdapter.setDefItem(position);
                }
            });
        }

    }

    private ReSetClickListener listener;

    public interface ReSetClickListener {

        void reSet();
    }

    public void setListener(ReSetClickListener listener) {
        this.listener = listener;
    }

    /**
     * 状态还原 清除选择的数据
     */
    private void reSetData() {
        parentAdapter.setDefItem(-1);
        parentAdapter.resetCheckDatas();
        childAdapter.setDefItem(-1);
        childAdapter.resetCheckDatas();
    }
}
